"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomState = exports.Room = void 0;
const MapCreater_1 = require("./MapCreater");
const WebSocket_1 = require("./WebSocket");
class Room {
    constructor(id, type) {
        this.state = RoomState.OPEN; //状态 
        this.arr_players = new Array(); //玩家
        /**
         * 房间内player收到消息
         */
        this.num = 0; //当前ready人数
        this.id = id;
        this.type = type;
    }
    //观战玩家
    //操作记录
    /**
     * 放进一个玩家
     */
    addPlayer(player) {
        const set = new Set(this.arr_players).add(player);
        this.arr_players = Array.from(set);
        player.roomId = this.id;
        if (this.arr_players.length == this.type) {
            this.prepare();
        }
    }
    /**
     * 人数足够，准备开始，先将地图信息传过去
     */
    prepare() {
        this.state = RoomState.PLAYING;
        this.arr_players.forEach((player, index) => {
            (0, WebSocket_1.sendObj)(player.connection, { other: "matching_success", seat: index });
            player.success();
        });
        const map = new MapCreater_1.MapCreater().createMap();
        this.broadcast({ method: "build", map, type: this.type });
    }
    /**
     * 减少玩家
     * 取消匹配
     */
    reducePlayer(player) {
        const set = new Set(this.arr_players);
        set.delete(player);
        this.arr_players = Array.from(set);
        if (this.arr_players.length == 0) {
            this.dissolution();
        }
    }
    /**
     * 解散
     */
    dissolution() {
        if (this.state == RoomState.CLOSE) {
            return;
        }
        console.log("房间解散： ", this.id);
        this.state = RoomState.CLOSE;
        this.arr_players.forEach(plyer => {
            (0, WebSocket_1.sendObj)(plyer.connection, { method: "dissolution" });
            plyer.roomId = -1;
            WebSocket_1.arr_hallPlayer.add(plyer);
        });
        this.arr_players = null;
        WebSocket_1.map_room.delete(this.id);
        (0, WebSocket_1.numOfHallPlayers)();
    }
    onMessage(obj) {
        switch (obj.method) {
            case "ready":
                this.num++;
                if (this.num == this.type) {
                    console.log("开始对局，房间号：" + this.id + "  房间人数：" + this.type);
                    this.broadcast({ method: "start" });
                    this.broadcast({ method: "turn", id: this.arr_players[0].id });
                }
                break;
            case "turn":
                let nextId = 0;
                for (let i = 0; i < this.arr_players.length; i++) {
                    if (obj.id == this.arr_players[i].id) {
                        if (i < this.arr_players.length - 1) {
                            nextId = i + 1;
                        }
                        break;
                    }
                }
                this.turn_player = this.arr_players[nextId];
                this.broadcast({ method: "turn", id: this.turn_player.id });
                break;
            case "over":
                this.dissolution();
                break;
            default: this.broadcast(obj);
        }
    }
    /**
     * 房间内广播
     */
    broadcast(obj) {
        this.arr_players.forEach(player => {
            (0, WebSocket_1.sendObj)(player.connection, obj);
        });
    }
    /**
     * 玩家掉线
     */
    dropLine(player) {
        //先让这个掉线的回合结束
        if (this.turn_player == player) {
            this.onMessage({ method: "turn", id: player.id });
        }
        //整理player数组
        for (let i = 0; i < this.type; i++) {
            if (this.arr_players[i] === player) {
                this.arr_players.splice(i, 1);
                break;
            }
        }
        //通知其他player掉线
        this.arr_players.forEach(player2 => {
            (0, WebSocket_1.sendObj)(player2.connection, { method: "dropLine", id: player.id });
        });
        //解散房间
        if (this.arr_players.length == 0) {
            this.dissolution();
        }
    }
}
exports.Room = Room;
var RoomState;
(function (RoomState) {
    RoomState[RoomState["OPEN"] = 0] = "OPEN";
    RoomState[RoomState["PLAYING"] = 1] = "PLAYING";
    RoomState[RoomState["CLOSE"] = 2] = "CLOSE";
})(RoomState = exports.RoomState || (exports.RoomState = {}));
